package dao;

import beans.Rebanho;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class RebanhoDAO {

    PreparedStatement pst;
    ResultSet rs;

    public RebanhoDAO() {
    }

    public Rebanho load(int ano, int mes) {
        Rebanho reb = new Rebanho();

        String query = "SELECT * FROM rebanho WHERE ano = ? AND mes = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, ano);
            pst.setInt(2, mes);
            rs = pst.executeQuery();

            if (rs.next()) {
                reb.setAno(rs.getInt("ano"));
                reb.setMes(rs.getInt("mes"));
            }

        } catch (SQLException ex) {
            ex.printStackTrace();

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }

        return reb;
    }

    public void insert(Rebanho reb) {
        String query = "INSERT INTO rebanho (ano, mes)"
                + " VALUES(?, ?)";

        try {
            pst = Conexao.getConexao().prepareStatement(query);

            pst.setInt(1, reb.getAno());
            pst.setInt(2, reb.getMes());

            pst.executeUpdate();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }
    }

   
    public void save(Rebanho reb) {
        String query = "SELECT COUNT(*) FROM rebanho WHERE ano = ? AND mes = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, reb.getAno());
            pst.setInt(2, reb.getMes());

            rs = pst.executeQuery();

            rs.next();

            if (rs.getInt("count") != 0) {
                insert(reb);
            }

        } catch (SQLException ex) {
            ex.printStackTrace();

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }
    }

    public void delete(Rebanho reb) {
        String query = "DELETE FROM rebanho WHERE ano = ? AND mes = ?";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            pst.setInt(1, reb.getAno());
            pst.setInt(2, reb.getMes());

            pst.executeUpdate();

        } catch (SQLException ex) {
            ex.printStackTrace();

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }
    }

    public ArrayList<Rebanho> loadAll() {
        ArrayList<Rebanho> rebanhos = new ArrayList<Rebanho>();

        String query = "SELECT * FROM rebanho";

        try {
            pst = Conexao.getConexao().prepareStatement(query);
            rs = pst.executeQuery();

            while (rs.next()) {
                Rebanho reb = new Rebanho();

                reb.setAno(rs.getInt("ano"));
                reb.setMes(rs.getInt("mes"));

                rebanhos.add(reb);
            }
        } catch (SQLException ex) {
            ex.getLocalizedMessage();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
            } catch (SQLException ex) {
                ex.getLocalizedMessage();
            }
        }

        return rebanhos;
    }
}
